/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.enums.PackScanOperTypeEnum;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.PackScanService;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.controller.common.BaseController;
import com.sigmatrix.h5.web.vo.PackScanOperationVo;
import com.sigmatrix.h5.web.vo.PackScanVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 *@ClassName: PackScanController
 *@Description: 包装管理扫码接口
 *@author youjun
 *@date 2017年8月16日 下午4:34:24
 */
@ApiIgnore
@Api(tags="包装扫码接口")
@RestController
@RequestMapping("/app/packscan")
public class PackScanController extends BaseController {
	
	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(PackScanController.class);

	/**包装管理业务层接口*/
	@Autowired
	private transient PackScanService packScanService;
	
	/**
	 *@Title: queryCode
	 *@Description: TODO()
	 *@return Response
	 *@author youjun
	 *@date 2017年8月16日 下午5:48:33
	 *@param request
	 *@param qRCodeVo
	 *@param result
	 *@return
	 */
	@ApiOperation(value = "包装管理扫码接口")
	@PostMapping("/scan")
	public Response scanCode(final HttpServletRequest request, @Valid @RequestBody final PackScanVo packScanVo, 
			final BindingResult result) {
	
		final Map<Integer, String> typeMap = PackScanOperTypeEnum.getAll();
		final String operationTypeDesc = typeMap.get(packScanVo.getOperationType());
		LOGGER.info("包装管理{}操作查询码接口开始...", operationTypeDesc);
		try {
			LOGGER.info("请求参数为:{}", JSONObject.toJSONString(packScanVo));
			if (result.hasErrors()) {
				final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
				LOGGER.error("包装管理{}操作查询码接口参数验证失败{}", operationTypeDesc, errInfo);
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
			}
			if(packScanVo.getCodeType()==null){
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "码类型不能为空");
			}
			return packScanService.scanCode(packScanVo);
		}catch(Exception e) {
			LOGGER.error("包装管理{}操作查询码接口失败", operationTypeDesc, e);
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
	
	/**
	 *@Title: scanOperationCode
	 *@Description: 扫码包装管理中实际操作的码
	 *@return Response
	 *@author youjun
	 *@date 2017年8月17日 下午3:23:51
	 *@param request
	 *@param packScanOperationVo
	 *@param result
	 *@return
	 */
	@ApiOperation(value = "包装管理扫描实际操作码接口")
	@PostMapping(value="/scanoperationcode")
	public Response scanOperationCode(final HttpServletRequest request, @Valid @RequestBody final PackScanOperationVo packScanOperationVo, 
			final BindingResult result) {
		
		final Map<Integer, String> typeMap = PackScanOperTypeEnum.getAll();
		final String operationTypeDesc = typeMap.get(packScanOperationVo.getOperationType());
		LOGGER.info("包装管理{}操作实际码查询码接口开始...", operationTypeDesc);
		try {
			LOGGER.info("请求参数为:{}", JSONObject.toJSONString(packScanOperationVo));
			if (result.hasErrors()) {
				final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
				LOGGER.error("包装管理{}操作查询码接口参数验证失败{}", operationTypeDesc, errInfo);
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
			}
			if(packScanOperationVo.getCodeType()==null){
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "码类型不能为空");
			}
			return packScanService.scanOperationCode(packScanOperationVo);
		}catch(Exception e) {
			LOGGER.error("包装管理{}操作查询码接口失败", operationTypeDesc, e);
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
	
}
